<?php
// +----------------------------------------------------------------------
// | 零云 [ 简单 高效 卓越 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.lingyun.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;

use Think\Page;

/**
 * 文章控制器
 * @author jry <598821125@qq.com>
 */
class ContractController extends AdminController
{
    /**
     * 默认方法
     * @author jry <598821125@qq.com>
     */
    public $target = array('1' => '品牌理念深度传播','2' => '新品上市','3'=>'VIP会员活动');
    public $place  = array('1' => '全程主题植入','2' => '制作过程','3'=>'环境摆放','4'=>'来宾使用');
    public function index()
    {
        $uid = session('user_auth.uid');
        $user_group   = D('Admin/Access')->getFieldByUid($uid, 'group'); 
        // 搜索
        $keyword         = I('keyword', '', 'string');
        $condition       = array('like', '%' . $keyword . '%');
        $map['id|brand_name'] = array($condition, $condition, '_multi' => true);

        
        $map['status'] = array('egt', '0'); // 禁用和正常状态
        if ($user_group==2) {
            $map['_string'] = "FIND_IN_SET('{$uid}',kol)";
        }elseif ($user_group==3) {
            $map['brand_id'] = $uid;
        }
        $post_object   = D('Admin/Contract');
        $data_list     = $post_object
            ->page($p, C('ADMIN_PAGE_ROWS'))
            ->where($map)
            ->order('id desc')
            ->select();
        $page = new Page(
            $post_object->where($map)->count(),
            C('ADMIN_PAGE_ROWS')
        );
        
        // 使用Builder快速建立列表页面。
        $builder = new \Common\Builder\ListBuilder();
        if ($user_group==1 or $user_group==3) {
            $builder->addTopButton('addnew', array('href' => U('add'))) // 添加新增按钮
            ->setSearch('请输入ID/品牌名', U('index'))
            ->addRightButton('course')  //提交课件
            ->addRightButton('edit'); // 添加编辑按钮
        }elseif ($user_group==2) {
            $builder->addRightButton('course');  //提交课件
        }elseif ($user_group==3) {
            $builder->addRightButton('course');  //提交课件
        }
        $builder->setMetaTitle('合同列表') // 设置页面标题
            //->addTableColumn('id', 'ID')
            //->addTableColumn('postname', '活动名')
            ->addTableColumn('brand_user', '品牌方')
            ->addTableColumn('brand_name', '品牌名')
            ->addTableColumn('product', '植入产品')
            ->addTableColumn('target_txt', '植入目地')
            ->addTableColumn('create_time', '创建时间', 'time')
            ->addTableColumn('update_time', '更新时间', 'time')
            ->addTableColumn('status', '状态', 'status')
            ->addTableColumn('right_button', '操作', 'btn')
            ->setTableDataList($data_list) // 数据列表
            ->setTableDataPage($page->show()) // 数据列表分页
            
            //->addRightButton('delete') // 添加删除按钮
            ->display();
    }

    /**
     * 新增文档
     * @author jry <598821125@qq.com>
     */
    public function add()
    {
        if (IS_POST) {
            $post_object = D('Admin/Contract');
            $data        = $post_object->create(format_data());
            if ($data) {
                $id = $post_object->add();
                if ($id) {
                    $this->success('新增成功', U('index'));
                } else {
                    $this->error('新增失败');
                }
            } else {
                $this->error($post_object->getError());
            }
        } else {
            $order_object = D('Admin/Contract');
            $map['status'] = array('egt', '0'); // 禁用和正常状态

            $access_object = D('Access');

            $data_list     = $access_object
            ->where($map)
            ->order('sort asc,id asc')
            ->select();
            $user_object  = D('User');
            foreach ($data_list as $key => $val) {
                $val['title']    = $user_object->getFieldById($val['uid'], 'username');
                if ($val['group']==2) {
                    $kol_list[$val['uid']] = $val;
                }elseif ($val['group']==3) {
                    $brand_list[$val['uid']] = $val;
                }
            }


            $tree = new \Util\Tree();
            $list = $tree->array2tree($data_list, 'title', 'uid', 'pid', 0, false);

          

            //转换成一维数组
            foreach ($list as $val) {
                $result[$val[$key]] = $val['title_show'];
            }

            // 使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('新增订单') // 设置页面标题
                ->setPostUrl(U('add')) // 设置表单提交地址
                
                //->addFormItem('post_id','select','活动名','活动名',select_list_as_tree('Admin/Post'))
                ->addFormItem('brand_id','select','品牌方','品牌方',$brand_list)
                ->addFormItem('brand_name','text','品牌名','品牌名')
                ->addFormItem('brand_txt','textarea','品牌介绍','品牌介绍')
                ->addFormItem('brand_upload','files','品牌介绍附件','品牌介绍附件')
                ->addFormItem('position','textarea','定位','定位')
                ->addFormItem('slogan','text','slogan','slogan')
                ->addFormItem('client_txt','textarea','品牌消费者','品牌消费者')
                ->addFormItem('client_upload','files','品牌消费者附件','品牌消费者附件')
                ->addFormItem('product','text','植入产品','植入产品')
                ->addFormItem('product_info_txt','textarea','产品信息/卖点','产品信息/卖点')
                ->addFormItem('product_info_upload','files','产品信息/卖点附件','产品信息/卖点附件')
                ->addFormItem('kv','picture','品牌理念','品牌理念')
                ->addFormItem('fonts','text','建议字体','建议字体')
                ->addFormItem('memo','kindeditor','其他','其他')
                ->addFormItem('target','checkbox','植入目的','植入目的',$this->target)
                ->addFormItem('kol','checkbox','合作KOL','合作KOL',$kol_list)
                ->addFormItem('classname','text','课程类型名','课程类型名')
                ->addFormItem('length','text','时长','时长')
                ->addFormItem('callscript','textarea','话术建议','话术建议')
                ->addFormItem('place','checkbox','植入环节','植入环节',$this->place)
                //->addFormItem('num', 'select', '场次(1000元/人次)', '场次',array('5' => 5, '10' => 10,'15'=>15,'20'=>20),'','id=num')
                ->display();
        }
    }

    /**
     * 编辑文章
     * @author jry <598821125@qq.com>
     */
    public function edit($id)
    {
        if (IS_POST) {
            $post_object = D('Admin/Contract');
            $data        = $post_object->create(format_data());
            if ($data) {
                $id = $post_object->save();
                if ($id !== false) {
                    $this->success('更新成功', U('index'));
                } else {
                    $this->error('更新失败');
                }
            } else {
                $this->error($post_object->getError());
            }
        } else {
            // 使用FormBuilder快速建立表单页面。
            $map['status'] = array('egt', '0'); // 禁用和正常状态

            $access_object = D('Access');
            $data_list     = $access_object
            ->where($map)
            ->order('sort asc,id asc')
            ->select();
            $user_object  = D('User');
            foreach ($data_list as $key => $val) {
                $val['title']    = $user_object->getFieldById($val['uid'], 'username');
                if ($val['group']==2) {
                    $kol_list[$val['uid']] = $val;
                }elseif ($val['group']==3) {
                    $brand_list[$val['uid']] = $val;
                }
            }


            $tree = new \Util\Tree();
            $list = $tree->array2tree($data_list, 'title', 'uid', 'pid', 0, false);

          

            //转换成一维数组
            foreach ($list as $val) {
                $result[$val[$key]] = $val['title_show'];
            }
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('编辑合同') // 设置页面标题
                ->setPostUrl(U('edit')) // 设置表单提交地址
                ->addFormItem('id', 'hidden', 'ID', 'ID')
                ->addFormItem('brand_id','select','品牌方','品牌方',$brand_list)
                ->addFormItem('brand_name','text','品牌名','品牌名')
                ->addFormItem('brand_txt','textarea','品牌介绍','品牌介绍')
                ->addFormItem('brand_upload','files','品牌介绍附件','品牌介绍附件')
                ->addFormItem('position','textarea','定位','定位')
                ->addFormItem('slogan','text','slogan','slogan')
                ->addFormItem('client_txt','textarea','品牌消费者','品牌消费者')
                ->addFormItem('client_upload','files','品牌消费者附件','品牌消费者附件')
                ->addFormItem('product','text','植入产品','植入产品')
                ->addFormItem('product_info_txt','textarea','产品信息/卖点','产品信息/卖点')
                ->addFormItem('product_info_upload','files','产品信息/卖点附件','产品信息/卖点附件')
                ->addFormItem('kv','picture','品牌理念','品牌理念')
                ->addFormItem('fonts','text','建议字体','建议字体')
                ->addFormItem('memo','kindeditor','其他','其他')
                ->addFormItem('target','checkbox','植入目的','植入目的',$this->target)
                ->addFormItem('kol','checkbox','合作KOL','合作KOL',$kol_list)
                ->addFormItem('classname','text','课程类型名','课程类型名')
                ->addFormItem('length','text','时长','时长')
                ->addFormItem('callscript','textarea','话术建议','话术建议')
                ->addFormItem('place','checkbox','植入环节','植入环节',$this->place)
                ->setFormData(D('Admin/Contract')->find($id))
                ->display();
        }
    }

    // 将树形列表转换为树
    public function list_as_tree($list, $extra = null, $key = 'id', $title_field = 'title')
    {
        //转换成树状列表(非严格模式)
        $tree = new \Util\Tree();
        $list = $tree->array2tree($list, $title_field, 'id', 'pid', 0, false);

        if ($extra) {
            $result[0] = $extra;
        }

        //转换成一维数组
        foreach ($list as $val) {
            $result[$val[$key]] = $val['title_show'];
        }
        return $result;
    }
}
